/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.orderView.dish;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

import com.ruipengkj.ApplicationContext;
import com.ruipengkj.bean.TOrder;
import com.ruipengkj.commons.util.MatcherFormatUtil;
import com.ruipengkj.commons.util.UUID;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.commons.util.display.ClientDisplay;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.component.jdialog.BorderJPanel;
import com.ruipengkj.component.jdialog.IDialog;

/**
 * 说明：
 * 确认支付Dialog
 * @Author：李智深
 * @Date：2012-03-20
 */
public class OrderPayDialog extends IDialog implements CaretListener{

	private static final long serialVersionUID = 1L;

	private JLabelJTextFieldComponent totalMoneyTF=null;//应收金额
	private JLabelJTextFieldComponent receivableMoneyTF=null;//实收金额
	private JLabelJTextFieldComponent discountTF=null;//整单折
	private JLabelJTextFieldComponent privilegeTF=null;//优惠
	private JLabelJTextFieldComponent receiptTF=null;//收款
	private JLabelJTextFieldComponent returnMoney;//应找金额
	
	private JButton playButton;//付款;
	private JButton celButtonl;//取消
	
	public boolean isSuccess=false;
	
	private JPanel main_jp;
	
	private TOrder order;
	
	public OrderPayDialog() {
		super((JDialog)null, "订单支付", true);
		this.init();
		this.setSize(300, 430);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
	}
	
	private void init(){
		this.initViews();
		this.initListener();
	}
	
	private void initViews(){
		main_jp=new BorderJPanel();
		BoxLayout boxLayout=new BoxLayout(main_jp,BoxLayout.Y_AXIS);
		main_jp.setLayout(boxLayout);
		
		discountTF=new JLabelJTextFieldComponent("整单折扣：",30);
		privilegeTF=new JLabelJTextFieldComponent("优惠金额：",30);
		totalMoneyTF=new JLabelJTextFieldComponent("应收金额：",30);
		receivableMoneyTF=new JLabelJTextFieldComponent("实收金额：",30);
		receivableMoneyTF.getText().setForeground(Color.RED);
		receiptTF=new JLabelJTextFieldComponent("收    款：",30);
		returnMoney = new JLabelJTextFieldComponent("应找金额：", 30);
		
		discountTF.getText().setText("无");//会员折扣
		privilegeTF.getText().setText("0.00");//优惠金额
		totalMoneyTF.getText().setText("0.00");//总 金 额
		receivableMoneyTF.getText().setText("0.00");//应收金额
		
		discountTF.getText().setEditable(false);
		privilegeTF.getText().setEditable(false);
		totalMoneyTF.getText().setEditable(false);
		receivableMoneyTF.getText().setEditable(false);
		returnMoney.getText().setEditable(false);
		
		JPanel jp1=uiFactory.createFlowJPanel();
		playButton=new JButton("结账");
		celButtonl=new JButton("取消");
		jp1.add(playButton);
		jp1.add(celButtonl);
		
		main_jp.add(discountTF);
		main_jp.add(privilegeTF);
		main_jp.add(totalMoneyTF);
		main_jp.add(receivableMoneyTF);
		main_jp.add(receiptTF);
		main_jp.add(returnMoney);
		main_jp.add(jp1);
		
		this.add(main_jp);
	}
	private void initListener(){
		playButton.addActionListener(this);
		celButtonl.addActionListener(this);
		receivableMoneyTF.getText().addKeyListener(this);
		receiptTF.getText().addKeyListener(this);
		receiptTF.getText().addCaretListener(this);
//		receiptTF.getText().addActionListener(this);
		receiptTF.getText().addMouseListener(this);
		this.addWindowListener(new WindowAdapter(){ 
			public void windowActivated(WindowEvent e){ 
				receiptTF.getText().requestFocus();
			}
		});
	}
	
	//显示金额，参数：应收金额，整单折
	public void showDialog(String TotalManay, String discount){
		totalMoneyTF.getText().setText(TotalManay+"");
		receivableMoneyTF.getText().setText(Util.DoubleHowMany(1, Double.parseDouble(TotalManay)*Double.parseDouble(discount))+"");//实收金额
		discountTF.getText().setText(discount);//折扣
		privilegeTF.getText().setText(Util.DoubleHowMany(1, Double.parseDouble(TotalManay) * (1-Double.parseDouble(discount)))+"");//优惠
		
		try {
			//输出客显  应付金额
			ClientDisplay.sendDisplay(ClientDisplay.DISPLAY_STATE_AMOUNT,receivableMoneyTF.getText().getText());
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.setVisible(true);
	}
	
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==playButton){
			//确定
			receiptTF.getText().setEditable(true);
			String maney=receiptTF.getText().getText().trim();
			if(!MatcherFormatUtil.MatcherPriceScheme(maney)){
				JOptionPane.showMessageDialog(null, "收款金额 不能为空，且小数点左边最多10位数字，右边最多2位数字");
				receiptTF.getText().setText("");
				receiptTF.requestFocus();
				return;
			}
			if(Double.parseDouble(maney)<Double.parseDouble(receivableMoneyTF.getText().getText().trim())){
				JOptionPane.showMessageDialog(null, "收款金额不能小于实收金额");
				receiptTF.requestFocus();
				return;
			}
			try {
				this.isSuccess=true;
				JOptionPane.showMessageDialog(null, "找零："+returnMoney.getText().getText());
				
				order = new TOrder();
				//输出客显  找零金额
				ClientDisplay.sendDisplay(ClientDisplay.DISPLAY_STATE_CHAGNE,returnMoney.getText().getText());
			} catch (Exception e1) {
				e1.printStackTrace();
				JOptionPane.showMessageDialog(null, "下单失败");
				this.isSuccess=false;
			}
			this.dispose();
		} else if(e.getSource()==celButtonl){
			//取消
			if(Util.showConfirmDialog("是否取消支付？")==0){
				this.dispose();
			}
		} else{
			super.actionPerformed(e);
		}
	}

	private TOrder getTOrder(){
		TOrder order=new TOrder();
		order.setWorkShiftID(ApplicationContext.WORKSHIFT_ID);
		order.setOrderID(UUID.GenUUID15());//定单编号CHAR(15)PK
		//支付方式
		order.setStatus(ApplicationContext.ORDER_STATUS07);//TINYINT定单状态1为开始创建的定单 2为确认定单 3为已确定支付方式 4为定单已分配（确认货源） 5为定单正在执行调度 6为定单已发货 7为定单已完成 该字节的10位为撤消标记 0为没有撤消，1为正在撤消
		order.setPayStatus(ApplicationContext.PLAY_STATUS_YES);//支付状态 1为未支付 2为已支付 
		order.setSendWay(ApplicationContext.ORDER_SENDWAY_MODT1);//配送方式  1为送货上门 2为物流
		order.setEndWay(ApplicationContext.ORDER_ENDWAY_MODT1);//结束方式0为未设置 1为成功完成 2为已合并 3为已取消 4为卖家已取消 5为卖家无法送抵（比如说买家不要了）
		order.setTotalAmount(Double.parseDouble(totalMoneyTF.getText().getText().trim()));//总金额DECIMAL(20,3)
		order.setNeedMoney(Double.parseDouble(receivableMoneyTF.getText().getText().trim()));//应支付金额
		order.setPayedAmount(Double.parseDouble(receivableMoneyTF.getText().getText().trim()));//已支付金额
		order.setDiscountAmount(Double.parseDouble(totalMoneyTF.getText().getText().trim())-Double.parseDouble(receivableMoneyTF.getText().getText().trim()));//优惠金额
//		if (tUser == null || tUser.getTShopMemberLevel() == null) {
//			order.setDiscountAmount(0);
//		} else {
//			order.setDiscountAmount(Double.parseDouble(totalMoneyTF.getText().getText())*tUser.getTShopMemberLevel().getDiscount());
//		}
		
		order.setPayID("");//支付单编号 CHAR(15)
		order.setOrderType(ApplicationContext.ORDERTYPE1);//定单类型 TINYINT 1为个人定单 21为企业定单（个人支付） 22为企业定单（企业支付） 3为楼宇定餐定单，当类型为3时，群组编号为定单生成时的位置编号
		order.setCommentStatus(ApplicationContext.COMMENNTSTATUS1);//评价状态 十位为1商店是否已评价，0为未评价 个位为1商品是否已评价，0为未评价
		order.setCancelReason("");//取消原因 VARCHAR(256)
		order.setNote("");//定单备注 VARCHAR(512)
		order.setSource(ApplicationContext.ORDER_SOURCE1);//订单来源CHAR(15)
		order.setType(ApplicationContext.ORDER_TYPE1+"");//订单类型CHAR(18)1为零售 2为批发
		order.setSaler(ApplicationContext.OPERATOR_ID);//销售人CHAR(15)
		order.setOperator(ApplicationContext.OPERATOR_ID);//操作人CHAR(15)
		order.setBillType(ApplicationContext.BILLTYPE1);//票据类型TINYINT 1为收据  2为发票
		order.setInvoice("");//发票号VARCHAR(30)
		order.setCardPayedAmount(0);//会员卡支持金额DECIMAL(20,3)
//		order.setSendInfo(null);//配送地址编号CHAR(15)
		return order;
	}
	
	//文本框里面的數值改變時
	@Override
	public void caretUpdate(CaretEvent e) {
		if (e.getSource() == receiptTF.getText()) {
			if (receiptTF != null && MatcherFormatUtil.MatcherPriceScheme(receiptTF.getText().getText()))
				returnMoney.getText().setText(Util.DoubleHowMany(1, Double.parseDouble(receiptTF.getText().getText())-
						Double.parseDouble(receivableMoneyTF.getText().getText()))+"");
			else 
				returnMoney.getText().setText("");
		}
	}
	
	@Override
	public void mouseClicked(MouseEvent e) {
		if (e.getSource() == receiptTF.getText()) {
			new PayJPopupMenu(receiptTF.getText()).show(receiptTF, receiptTF.getWidth(), 0);
		}
	}

	public TOrder getOrder() {
		return order;
	}
	
	public static void main(String[] args) {
		new OrderPayDialog().setVisible(true);
	}
}

